package com.winit.project.core.set;

import com.alibaba.fastjson.JSONObject;
import com.winit.project.core.bean.PackageTypeEnum;
import com.winit.project.core.bean.VcsTypeEnum;
import com.winit.project.util.ProjectUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

/**
 * Created by wangjian on 16/9/6.
 */
public class ProjectSetConfig {

    private static final Logger logger = LoggerFactory.getLogger(ProjectSetConfig.class);
    private static String INFO_FILE_NAME = "info.json";

    private String projectName;

    private String vcsType = VcsTypeEnum.SVN.getDesc();//默认为svn
    private String packageType;//maven_war maven_jar php
    private String subModule;
    private String path;
    //指向统一配置中心的工程名称,比如oms2
    private String configProjectName;
    private String warName;
    //php工程压缩包名称
    private String packageName;

    public ProjectSetConfig(String projectName,boolean loadInfo) throws Exception {
        if(StringUtils.isEmpty(projectName)){
            logger.error("工程名不能为空");
            throw new Exception("工程名不能为空");
        }
        this.projectName = projectName;
        this.path = ProjectUtil.getTrunkFilePath(projectName);
        if(loadInfo){
            this.loadFromFile();
        }
    }

    public boolean saveToFile() throws Exception{
        JSONObject object = new JSONObject();
        object.put("vcsType",this.vcsType);
        object.put("packageType",this.packageType);
        object.put("subModule",this.subModule);
        object.put("configProjectName",this.configProjectName);
        object.put("warName",this.warName);
        object.put("packageName",this.packageName);
        return ProjectUtil.saveToFile(this.path+INFO_FILE_NAME,object);
    }

    public boolean loadFromFile() throws Exception{
        JSONObject object = ProjectUtil.loadFromFile(this.path+INFO_FILE_NAME);
        this.vcsType = object.getString("vcsType");
        this.packageType = object.getString("packageType");
        this.subModule = object.getString("subModule");
        this.configProjectName = object.getString("configProjectName");
        this.warName = object.getString("warName");
        this.packageName = object.getString("packageName");
        return true;
    }

    public String getPackageType() {
        return packageType;
    }

    public void setPackageType(PackageTypeEnum packageType) {
        this.packageType = packageType.getDesc();
    }

    public void setPackageType(String packageType){
        this.packageType = packageType;
    }

    public String getSubModule() {
        if(StringUtils.isEmpty(this.subModule)){
            return "";
        }
        return subModule;
    }

    public void setSubModule(String subModule) {
        this.subModule = subModule;
    }

    public String getConfigProjectName() {
        if(StringUtils.isEmpty(this.configProjectName)){
            return this.projectName;
        }
        return configProjectName;
    }

    public void setConfigProjectName(String configProjectName) {
        this.configProjectName = configProjectName;
    }

    public String getWarName() {
        if(StringUtils.isEmpty(this.warName)){
            return this.projectName;
        }
        return warName;
    }

    public void setWarName(String warName) {
        this.warName = warName;
    }

    public String getVcsType() {
        return vcsType;
    }

    public void setVcsType(VcsTypeEnum vcsType) {
        this.vcsType = vcsType.getDesc();
    }

    public String getProjectName() {
        return projectName;
    }

    public String getPackageName() {
        return packageName;
    }

    public void setPackageName(String packageName) {
        this.packageName = packageName;
    }
}
